package com.lgCore.util;/**
 * Created by by on 2020/11/11 14:01
 */

import com.google.protobuf.ByteString;
import com.lgCore.protoModel.ProtoMsg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 *@program: sxreader_rpc
 *@author: ligang
 *@create: 2020-11-11 14:01
 */
public class ProtoDemo {

    public static void main(String[] args) throws IOException {
//        // 按照定义的数据结构，创建一个对象
//        UserProto.User.Builder userInfo = UserProto.User.newBuilder();
//        userInfo.setSubReqId(1);
//        userInfo.setUserName("ligang");
//        userInfo.setProductName("李刚");
//        userInfo.addAddress("3333");
//        UserProto.User userMsg = userInfo.build();
//        // 将数据写到输出流
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        userMsg.writeTo(output);
//        // 将数据序列化后发送
//        byte[] byteArray = output.toByteArray();
//        // 接收到流并读取
//        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
//        // 反序列化
//        UserProto.User userInfo2 = UserProto.User.parseFrom(input);
//        System.out.println("userName:" + userInfo2.getUserName());
//        System.out.println("productName:" + userInfo2.getProductName());
//        System.out.println("subReqId:" + userInfo2.getSubReqId());


//        // 按照定义的数据结构，创建一个对象
//        RpcMsgProto.RpcMsg.Builder rpcMsg = RpcMsgProto.RpcMsg.newBuilder();
//        rpcMsg.setClientId("111111");
//        rpcMsg.setBusinessId("222222");
//        rpcMsg.setMsgType("2");
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("aa","11");
//        jsonObject.put("bb","11");
//        rpcMsg.addMsgData(ByteString.copyFrom(jsonObject.toJSONString().getBytes()));
//        RpcMsgProto.RpcMsg rpcMsg1 = rpcMsg.build();
//        // 将数据写到输出流
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        rpcMsg1.writeTo(output);
//        // 将数据序列化后发送
//        byte[] byteArray = output.toByteArray();
//        // 接收到流并读取
//        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
//        // 反序列化
//        RpcMsgProto.RpcMsg rpcMsg2 = RpcMsgProto.RpcMsg.parseFrom(input);
//        System.out.println("ClientId:" + rpcMsg2.getClientId());
//        System.out.println("BusinessId:" + rpcMsg2.getBusinessId());
//        System.out.println("msgData:" + rpcMsg2.getMsgDataList());
//
//        System.out.println("msgData:" + new String(rpcMsg2.getMsgDataList().get(0).toByteArray()));


        ProtoMsg.Message.Builder message = ProtoMsg.Message.newBuilder();

        //登录请求
        ProtoMsg.LoginRequest.Builder loginRequestBuilder= ProtoMsg.LoginRequest.newBuilder();
        loginRequestBuilder.setClientId("唯一客户id");
        loginRequestBuilder.setUserName("ligang");
        loginRequestBuilder.setPassword("2222");
        loginRequestBuilder.setType(ProtoMsg.HeadType.LOGIN_REQUEST);
        message.setLoginRequest(loginRequestBuilder);
        //登录返回
        ProtoMsg.LoginResponse.Builder loginResponse= ProtoMsg.LoginResponse.newBuilder();
        loginResponse.setClientId("唯一客户id");
        loginResponse.setCode("200");
        loginResponse.setResult(true);
        loginResponse.setErrorInfo("错误信息");
        loginResponse.setType(ProtoMsg.HeadType.LOGIN_REQUEST);
        message.setLoginRequest(loginRequestBuilder);

        //rpc请求
        ProtoMsg.RpcRequest.Builder rpcRequest= ProtoMsg.RpcRequest.newBuilder();
        rpcRequest.setClientId("唯一客户id");
        rpcRequest.setServiceName("appService");
        rpcRequest.setMethodName("test");
        Object[] objects = new Object[2];
        objects[0]="11111";
        objects[1]=23;


        rpcRequest.setRequestObjects(ByteString.copyFrom(ByteObjUtil.objToBytes(objects)));
        rpcRequest.setType(ProtoMsg.HeadType.RPC_REQUEST);
        message.setRpcRequest(rpcRequest);
        //rpc响应
        ProtoMsg.RpcResponse.Builder rpcRespopnse= ProtoMsg.RpcResponse.newBuilder();
        rpcRespopnse.setClientId("唯一客户id");
        rpcRespopnse.setResponse(ByteString.copyFrom(ByteObjUtil.objToBytes("1222222222222222222")));
        rpcRespopnse.setIsSucess(true);
        rpcRespopnse.setType(ProtoMsg.HeadType.RPC_RESPONSE);
        message.setRpcResponse(rpcRespopnse);



//        ProtoMsg.LoginRequest loginRequestEntity = loginRequestBuilder.build();
//        // 将数据写到输出流
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        loginRequestEntity.writeTo(output);
//        // 将数据序列化后发送
//        byte[] byteArray = output.toByteArray();
//        // 接收到流并读取
//        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
//        // 反序列化
//        ProtoMsg.LoginRequest loginRequestEntity2 = ProtoMsg.LoginRequest.parseFrom(input);
//        System.out.println("msgData:" + loginRequestEntity2);
//        System.out.println("clientId:" + loginRequestEntity2.getClientId());




        ProtoMsg.RpcRequest rpcRequest1 = rpcRequest.build();
        System.out.println(rpcRequest1);
        // 将数据写到输出流
        Date date = new Date();
        int num=100000;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] byteArray =null;
        for(int i=0;i<num;i++){
            output = new ByteArrayOutputStream();
            rpcRequest1.writeTo(output);
            // 将数据序列化后发送
            byteArray = output.toByteArray();
        }
        System.out.println("10万次序列化时间："+(new Date().getTime()-date.getTime()));
//        System.out.println(byteArray.length);
        output = new ByteArrayOutputStream();
        rpcRequest1.writeTo(output);
//         将数据序列化后发送
        byteArray = output.toByteArray();
        // 接收到流并读取
        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
        // 反序列化
        ProtoMsg.RpcRequest rpcRequest2=null;
        date = new Date();
        for(int i=0;i<num;i++){
            rpcRequest2 = ProtoMsg.RpcRequest.parseFrom(input);
        }
        System.out.println("10万次解序列化时间："+(new Date().getTime()-date.getTime()));
        System.out.println("msgData:" + rpcRequest2);
        System.out.println("msgData:" + rpcRequest2);
        System.out.println("clientId:" + rpcRequest2.getRequestObjects());
//        byte[] bytes =  rpcRequest2.getRequestObjects().toByteArray();
//        Object ojb = ByteObjUtil.bytesToObject(bytes);
//        System.out.println("ojb:" + ojb);
    }
}
